package com.ruoyi.business.domain.enums;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单来源枚举
 * 标识订单来自哪个商城系统
 */
public enum OrderSource {

    /**
     * 普通商城 - 纯商品交易
     */
    NORMAL_MALL(1, "普通商城", "普通商品交易商城"),

    /**
     * 积分商城 - 积分兑换商城
     */
    POINTS_MALL(2, "积分商城", "积分兑换商城");

    private final Integer code;
    private final String name;
    private final String description;

    OrderSource(Integer code, String name, String description) {
        this.code = code;
        this.name = name;
        this.description = description;
    }

    /**
     * 根据code获取枚举
     */
    public static OrderSource getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (OrderSource source : values()) {
            if (source.getCode().equals(code)) {
                return source;
            }
        }
        throw new IllegalArgumentException("未知的订单来源码: " + code);
    }

    /**
     * 根据name获取枚举
     */
    public static OrderSource getByName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return null;
        }
        for (OrderSource source : values()) {
            if (source.getName().equals(name.trim())) {
                return source;
            }
        }
        throw new IllegalArgumentException("未知的订单来源名称: " + name);
    }

    /**
     * 检查是否为积分商城
     */
    public boolean isPointsMall() {
        return this == POINTS_MALL;
    }

    /**
     * 检查是否为普通商城
     */
    public boolean isNormalMall() {
        return this == NORMAL_MALL;
    }

    /**
     * 获取支持的订单类型
     */
    public List<OrderType> getSupportedOrderTypes() {
        List<OrderType> types = new ArrayList<>();

        switch (this) {
            case NORMAL_MALL:
                types.add(OrderType.NORMAL);
                break;
            case POINTS_MALL:
                types.add(OrderType.POINTS_ONLY);
                types.add(OrderType.POINTS_AND_MONEY);
                types.add(OrderType.KOC_EXCHANGE);
                break;
        }

        return types;
    }

    /**
     * 验证订单来源和类型的兼容性
     */
    public boolean supportsOrderType(OrderType orderType) {
        return getSupportedOrderTypes().contains(orderType);
    }

    // Getters
    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return String.format("OrderSource{code=%d, name='%s', description='%s'}",
            code, name, description);
    }
}
